package com.hqd.jjvm.jnative.lang;

import com.hqd.jjvm.jnative.JNativeRegistry;
import com.hqd.jjvm.rtda.JThread;
import com.hqd.jjvm.rtda.StackFrame;
import com.hqd.jjvm.rtda.slot.NumberSlot;
import lombok.Getter;

@Getter
public class JFloatNativeRegistry extends JNativeRegistry {
    private static final JFloatNativeRegistry instance = new JFloatNativeRegistry();
    protected static final String CLASS_PATH = "java/lang/Float";

    static {
        registry(CLASS_PATH, "floatToRawIntBits", "(F)I", JFloatNativeRegistry::floatToRawIntBits);
        registry(CLASS_PATH, "intBitsToFloat", "(I)F", JFloatNativeRegistry::intBitsToFloat);
    }

    protected static void intBitsToFloat(JThread jThread) {
        StackFrame stackFrame = jThread.getJvmStack().getTop();
        int bits = stackFrame.getLocalVarsIntVal(0);
        stackFrame.push(new NumberSlot(Float.intBitsToFloat(bits)));
    }

    protected static void floatToRawIntBits(JThread jThread) {
        StackFrame stackFrame = jThread.getJvmStack().getTop();
        float val = stackFrame.getLocalVars().getFloatVal(0);
        stackFrame.push(new NumberSlot(Float.floatToRawIntBits(val)));
    }

    public static JFloatNativeRegistry getInstance() {
        return instance;
    }
}
